<?php
namespace EM\NewsBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Doctrine\ORM\EntityRepository;

class NewType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder->add('title', 'text', array('attr' => array('class' => 'validate[required,maxSize[250]]', 'placeholder' => 'Title goes here') ))
                ->add('titleSeo', 'text', array('attr' => array('class' => 'validate[required,maxSize[250]]')))
                ->add('content', 'textarea', array('attr' => array('rows' => '8' ) ))
                ->add('metaDescription', 'textarea',array('attr' => array('data-maxlength' => '156')))
		->add('status', 'choice', array('empty_value' => 'Choose a status',
						'choices'   => array(
							'P'   => 'Revision',
							'Dr' => 'Draft'
						),
						'expanded'  => false,
						'multiple'  => false,
					))
                ->add('keywords', 'hidden')
		 
            ;
        $builder->addEventListener(FormEvents::PRE_SET_DATA, function(FormEvent $event){
        $news = $event->getData();
        $form = $event->getForm();
        if($news->getKeywords()=="1"){
//        $form->add('geography', 'entity', array( 'empty_value' => 'Choose an option','label' => 'Country', 
//            'required' => true, 'multiple' => false,'expanded'=>false, 
//            'class' => 'EMNewsBundle:Geography',
//           // 'choices' => $news->findBy
//            'query_builder' => function(EntityRepository $er) {return $er->createQueryBuilder('u')->where('u.level = 2');},
//            'property' => 'name'));
////            echo $news->getGeography()->getName();
////            exit();
//        }
//        else
//        {
        $form->add('url', 'text' /*array('attr' => array('class' => 'validate[required,maxSize[15]]') )*/)
        ->add('image', 'text' /*array('attr' => array('class' => 'validate[required,maxSize[15]]') )*/)
        ->add('source', 'entity',array('label' => 'Country: ',
						'empty_value' => 'Choose a source',
						'class' => 'EMNewsBundle:Source',
						'property' => 'name',
						'attr' => array('class' => 'validate[required]')
						));     
        }
            });	
    }
    public function getName()
    {
        return 'news_form';
    }
	
}

